<div class="usercenter">
	<include file="User/_menu" />
	<div class="content">
		<div class="title">管理收货地址</div>
		<form method="post">
		<input id="address" type="hidden" name="address" />
		<table class="addr">
			<tr><th width="95">收件人：</th><td><input type="text" value="{$addr.consignee}" name="consignee" /></td></tr>
			<tr><th>收件地区：</th><td>
				<select id="province"></select> <select id="city"></select> <select id="area"></select>
				</td></tr>
			<tr><th>详细地址：</th><td><input id="addr" type="text" /></td></tr>
			<tr><th>手机：</th><td><input type="text" value="{$addr.phone}" name="phone" /></td></tr>
			<tr><th>邮箱：</th><td><input type="text" value="{$addr.email}" name="email" /></td></tr>
			<tr><th></th><td class="act">
				<input type="submit" value="保存" id="submit" />
				<input type="button" value="取消" onclick="location.href='{:U('user/addr')}'" />
			</td></tr>
		</table>
		</form>
	</div>
</div>
<script src="__PUBLIC__/common/js/chinaArea.js"></script>
<script>
//填充地区三级下拉菜单
(function(){
	var data = chinaArea(); //载入JSON地区数据
	var $province = $("#province");
	var $city = $("#city");
	var $area = $("#area");
	//1级下列菜单的change事件
	$province.change(function(){
		var id = $(this).val();
		if(id==="0" || $(this).find('option:selected').text()==="其他"){
			$city.hide();
			$area.hide();
		}else{
			fillSelect($city,data[id].sub);
			$city.show();
			$area.hide();
		}
	});
	//2级下列菜单的change事件
	$city.change(function(){
		var id = $(this).val();
		var pid = $province.val();
		if(id==="0" || $(this).find('option:selected').text()==="其他"){
			$area.hide();
		}else if(data[pid].sub[id].sub!==undefined){
			fillSelect($area,data[pid].sub[id].sub);
			$area.show();
		}
	});
	fillSelect($province,data); //自动填充1级菜单
	$province.change(); //自动载入第1个下拉菜单
	//自动填充下拉菜单
	function fillSelect($select,data){
		$select.html(''); //清空原有的数据
		for(var i in data){
			$select.append('<option value="'+i+'">'+data[i].name+'</option>');
		}
	}
})();
//自动填写收货地址
(function(){
	var auto = {:json_encode($addr['area'])};
	if(auto.length>3){
		$("#province").find('option:contains("'+auto[0]+'")').attr("selected","selected").change();
		$("#city").find('option:contains("'+auto[1]+'")').attr("selected","selected").change();
		$("#area").find('option:contains("'+auto[2]+'")').attr("selected","selected");
		$("#addr").val(auto[3]);
	}
})();
//提交表单时检查并拼接完整地址
$("#submit").click(function(){
	var pro_val = $("#province:visible").find("option:selected").text();
	var city_val = $("#city:visible").find("option:selected").text();
	var area_val = $("#area:visible").find("option:selected").text();
	var addr = $("#addr").val();
	if(pro_val === '请选择' || city_val === '请选择' || area_val === '请选择' || $.trim(addr)===""){
		$.showTip("请输入正确的地址");
		return false;
	}
	$("#address").val(pro_val+','+city_val+','+area_val+','+addr);
});
</script>